package com.fresh.auth.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fresh.auth.Service.UserService;
import com.fresh.auth.mapper.UserMapper;
import com.fresh.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.selectOne(new QueryWrapper<User>()
                .eq("user_name",user.getUserName()).eq("user_password",user.getUserPassword()));
    }

    @Override
    public Integer getUserId(User user) {
        User found = userMapper.selectOne(new QueryWrapper<User>()
                .eq("user_name", user.getUserName()));
        return found.getUserId();
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void delete(User user) {
        userMapper.deleteById(user.getUserId());
    }

    @Override
    public void updateN(User user) {
        userMapper.updateById(user);
    }

    @Override
    public PageInfo<User> all(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list = userMapper.selectList(null);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<User> getPageByCondition(User user, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list = userMapper.getByCondition(user);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
